package com.roadjava.rating.controller;


import com.roadjava.rating.bean.entity.RatingDO;
import com.roadjava.rating.bean.req.rating.RatingReq;
import com.roadjava.rating.bean.res.Result;
import com.roadjava.rating.config.HoneyLogs;
import com.roadjava.rating.enums.LogEnum;
import com.roadjava.rating.security.RateLimitService;
import com.roadjava.rating.service.RatingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ratings")
public class RatingController {

    @Autowired
    private RatingService ratingService;

    // 引入 RateLimitService
    private final RateLimitService rateLimitService = new RateLimitService();

    @HoneyLogs(operation = "添加评分",type = LogEnum.ADD)
    // 添加评分
    @PostMapping
    public Result<RatingReq> addRating(@RequestBody RatingReq rating) {
        String userId = rating.getId().toString(); // 假设 RatingReq 中有用户 ID
        if (!rateLimitService.isAllowed(userId)) {
            return Result.buildFailure("Too many requests, please try again later.");
        }

        ratingService.addRating(rating);
        return Result.buildSuccess(rating);
    }

    @HoneyLogs(operation = "更新评分",type = LogEnum.UPDATE)
    // 更新评分
    @PutMapping
    public Result<Void> updateRating(@RequestParam Integer ratingId, @RequestParam Integer score) {
        // 假设可以通过评分 ID 获取用户 ID
        Long userId = Long.valueOf(ratingId); // 获取用户 ID
        if (!rateLimitService.isAllowed(userId.toString())) {
            return Result.buildFailure("Too many requests, please try again later.");
        }

        ratingService.updateRating(ratingId, score);
        return Result.buildSuccess();
    }

    @HoneyLogs(operation = "删除评分",type = LogEnum.DELETE)
    // 删除评分
    @DeleteMapping
    public Result<Void> deleteRating(@RequestParam Integer ratingId) {
        Long userId = Long.valueOf(ratingId.toString()); // 假设通过评分 ID 获取用户 ID
        if (!rateLimitService.isAllowed(userId.toString())) {
            return Result.buildFailure("Too many requests, please try again later.");
        }

        ratingService.deleteRating(ratingId);
        return Result.buildSuccess();
    }

    // 根据资源ID和用户ID获取评分
    @GetMapping
    public Result<RatingDO> getRatingByResourceIdAndUserId(@RequestParam Integer resourceId, @RequestParam Long userId) {
        RatingDO rating = ratingService.getRatingByResourceIdAndUserId(resourceId, userId);
        return Result.buildSuccess(rating);
    }

    // 获取某个资源的平均评分
    @GetMapping("/average")
    public Result<Double> getAverageRating(@RequestParam Integer resourceId) {
        Double averageRating = ratingService.getAverageRating(resourceId);
        return Result.buildSuccess(averageRating);
    }

    // 获取所有评分
    @GetMapping("/all")
    public Result<List<RatingDO>> getAllRatings() {
        List<RatingDO> ratings = ratingService.getAllRatings();
        return Result.buildSuccess(ratings);
    }

    // 根据资源ID获取评分
    @GetMapping("/resource")
    public Result<List<RatingDO>> getRatingsByResourceId(@RequestParam Integer resourceId) {
        List<RatingDO> ratings = ratingService.getRatingsByResourceId(resourceId);
        return Result.buildSuccess(ratings);
    }

    // 根据用户ID获取评分
    @GetMapping("/user")
    public Result<List<RatingDO>> getRatingsByUserId(@RequestParam Long userId) {
        List<RatingDO> ratings = ratingService.getRatingsByUserId(userId);
        return Result.buildSuccess(ratings);
    }
}
